package com.ceba.test.base.config;

import abc.Config;
import abc.ID;
import abc.util.DateUtils;
import com.ceba.base.enums.FileStorageFalgeEnum;
import com.ceba.base.utils.IDSFileUtils;
import com.ceba.cebautils.DateUtil;
import com.ceba.cebautils.FileUtils;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.language.resources.WebMessages;
import com.ceba.test.modules.storage.entity.GStorageAddress;
import com.ceba.test.modules.storage.logicService.StorageFileCacheManage;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.util.Date;

public class LogicPathUtil extends CebaPathUtil{

    /**
     *   获取上传文件的路径
     * @param id
     * @return
     */
    public static String getSystemUploadPath(String id) {
        if (CebaConfig.FILE_STORAGE_FALG == FileStorageFalgeEnum.START) {
            if (StringUtils.isNotBlank(id)) {
                return StorageFileCacheManage.getIDPath(id) + "/";
            }
            return StorageFileCacheManage.getSystemUsePath() + "/";
        } else {
            return  systemValitPath();
        }
    }

    //日志路径 -- DEBUG
    private static String LOGS_PHYSICAL_FILE_FOLDER(String id) {
        return getSystemUploadPath(id) + LOGS + "/";
    }

    //日志路径 -- ERROR
    private static String S_LOGS_PHYSICAL_FILE_FOLDER(String id) {
        return getSystemUploadPath(id) + S_LOGS + "/";
    }

    /**
     * 取得浏览器访问的路径
     *
     * @param virtualPath 数据库 虚拟路径  upfile/..../test.pdf
     * @return
     */
    public static String getBrowserPath(String gStorageAddressId, String virtualPath) {
        if (CebaConfig.FILE_STORAGE_FALG == FileStorageFalgeEnum.START) {
            if (StringUtils.isEmpty(gStorageAddressId)) {
                gStorageAddressId = StorageFileCacheManage.getSystemUseAddressId();
            }
            return WebMessages.getContextPath() + GStorageAddress.filterCreateRule(gStorageAddressId) + "/" + virtualPath;
        }
        return WebMessages.getContextPath() + virtualPath;

    }




    /**
     * 根据  浏览器 路径   取得存储ID
     *
     * @param browserPath /ep/fileVisit/存储Id/upfil/.....pdf
     * @return
     */
    public static String browserPathToStorageAddressId(String browserPath) {
        if (StringUtils.isEmpty(browserPath)) {
            return null;
        }
        String stringStr = StringUtils.substring(browserPath, CONTEXT_PATH.length());
        return GStorageAddress.filterGetStorageAddressId(stringStr);
    }


    /**
     * 浏览器 路径  转  虚拟路径
     *
     * @param browserPath /ep/fileVisit/存储Id/upfil/.....pdf
     * @return
     */
    public static String browserPathToVirtualPath(String browserPath) {
        if (StringUtils.isEmpty(browserPath)) {
            return null;
        }
        int length;
        if(CebaConfig.FILE_STORAGE_FALG.equals(FileStorageFalgeEnum.NOSTART)){
             length = CONTEXT_PATH.length();
        }else{
            length = CONTEXT_PATH.length() + GStorageAddress.getFilterLength() + 1;//===/qd/fileVisit/存储Id/  的长度
        }
        return StringUtils.substring(browserPath, length);
    }

    /**
     * 浏览器路径转绝对路径
     *
     * @param // path /ep/fileVisit/存储Id/upfil/.....pdf
     * @return
     */
    public static String getBrowserPathToAbsolutelyPath(String browserPath) {
        if (CebaConfig.FILE_STORAGE_FALG == FileStorageFalgeEnum.START) {
            String storageAddressId = browserPathToStorageAddressId(browserPath);
            String virtualPath = browserPathToVirtualPath(browserPath);// upfile/xxx.pdf
            return getSystemUploadPath(storageAddressId) + virtualPath;
        } else {
            if (StringUtils.isNotBlank(browserPath)) {
                return Config.UPLOAD_PATH + browserPath;
            }
        }
        return null;
    }


    /**
     * 取得文件的绝对路径
     * @param fileRelativePath 文件相对路径  upfile/...../test.pdf
     * @return
     */
    public static String getAbsolutelyPath(String gStorageAddressId,String fileRelativePath){
        if(fileRelativePath.indexOf(WebMessages.getContextPath())>-1){
            fileRelativePath = fileRelativePath.replace(WebMessages.getContextPath(), "");
        }
        return getSystemUploadPath(gStorageAddressId)+fileRelativePath;
    }


    /**
     * 根据绝对路径取得虚拟路径
     * @param filePath
     * @return
     */
    public static String getVirtualPath(String gStorageAddressId,String filePath){
        if(StringUtils.isNotBlank(filePath)){
            return filePath.replace(getSystemUploadPath(gStorageAddressId), "");
        }
        return null;
    }




    //根据用户id获取用户文件物理存放路径
    public static String getFolderUserPath(String gStorageId ,String userId) {
        return getSystemUploadPath(gStorageId) +VIRTUAL_USER_FOLDER+ userId + "/" + DateUtil.getDayTime(DateUtil.YYYY_MM_DD_, 0) + "/";
    }


    //获取临时目录物理路径
	public static String getPhysicalTempDirPath(String gStorageId) {
		if (!new File(getSystemUploadPath(gStorageId)+VIRTUAL_TEMP_FOLDER).exists()) {
			new File(getSystemUploadPath(gStorageId)+VIRTUAL_TEMP_FOLDER).mkdirs();
		}
		return getSystemUploadPath(gStorageId)+VIRTUAL_TEMP_FOLDER;
	}


    /**
     * 根据用户Id获取缓存目录
     *
     * @param userId
     * @return
     */
    public static String getPhysicalTempFolderByUserId(String gStorageId, String userId) {
        String path = getPhysicalTempDirPath(gStorageId) + userId + "/";
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        return path;
    }

    //TOPDF文件存放物理位置
    public static String getPhysicalToPDFFolder(String gStorageId) {
        if (!new File(getSystemUploadPath(gStorageId)+VIRTUAL_TOPDF_FOLDER).exists()) {
            new File(getSystemUploadPath(gStorageId)+VIRTUAL_TOPDF_FOLDER).mkdirs();
        }
        return getSystemUploadPath(gStorageId)+VIRTUAL_TOPDF_FOLDER;
    }


    //获取用户文件物理路径
    public static String getPhysicalUserFolder(String gStorageId,String userId, String fileName) {
        String path = getSystemUploadPath(gStorageId)+VIRTUAL_USER_FOLDER + userId + "/" + fileName;
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        return path;
    }

    public static String getFolderOfUser(String gStorageId,String userId){
        String path=getPhysicalUserFolder(gStorageId,userId, "") + DateUtils.formatDate(new Date()) + "/";
        IDSFileUtils.createDir(path);
        return  path;
    }

    //根据用户ID获取报告文件的物理保存路径
    public static String getPhysicalSignFolderByUserId(String gStorageId, String userId) {
        return getSystemUploadPath(gStorageId)+ VIRTUAL_SIGN_PATH_FOLDER + userId + "/";
    }

    //根据签名文件的物理保存路径
    public static String getPhysicalSignFolder(String gStorageId) {
        return getSystemUploadPath(gStorageId)+VIRTUAL_SIGN_PATH_FOLDER;
    }

    //物理缓存路径
    public static String getPhysicalTempFolder(String gStorageId ) {
        if (!new File( getSystemUploadPath(gStorageId)+VIRTUAL_TEMP_FOLDER).exists()) {
            new File(getSystemUploadPath(gStorageId)+VIRTUAL_TEMP_FOLDER).mkdirs();
        }
        return getSystemUploadPath(gStorageId)+VIRTUAL_TEMP_FOLDER;
    }


    //License存放路径
    public  static  String getPhySicalLicenseFolder( String gStorageId){
        if (!new File(getSystemUploadPath(gStorageId)+VIRTUAL_LICENSE+ DateUtils.formatDate(new Date()) + "/").exists()) {
            new File(getSystemUploadPath(gStorageId)+VIRTUAL_LICENSE+ DateUtils.formatDate(new Date()) + "/").mkdirs();
    }
        return getSystemUploadPath(gStorageId)+VIRTUAL_LICENSE+ DateUtils.formatDate(new Date()) + "/";
    }



    //模板文件存放物理位置
    public static String getPhysicalTemplateFolder(String gStorageId) {
        if (!new File(getSystemUploadPath(gStorageId)+VIRTUAL_TEMPLATE_FOLDER).exists()) {
            new File(getSystemUploadPath(gStorageId)+VIRTUAL_TEMPLATE_FOLDER).mkdirs();
        }
        return getSystemUploadPath(gStorageId)+VIRTUAL_TEMPLATE_FOLDER;
    }


    //基础版文件存储物理位置--当天的
    public static String getTodayPhysicalSignFmFolder(String gStorageId) {
        String path = getSystemUploadPath(gStorageId)+SIGN_FM_FOLDER + DateUtil.getDayTime(null, 0) + "/" + ID.getGuid() + "/";
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        return path;
    }



    //物理图片路径
    public static String getPhysicalPicFolder(String  gStorageId) {
        if (!new File(getSystemUploadPath(gStorageId)+VIRTUAL_IMAGE_FOLDER).exists()) {
            new File(getSystemUploadPath(gStorageId)+VIRTUAL_IMAGE_FOLDER).mkdirs();
        }
        return getSystemUploadPath(gStorageId)+VIRTUAL_IMAGE_FOLDER;
    }

    /**
     * 根据相对路径取得模板文件的绝对路径
     *
     * @param virtual_path
     * @return
     */
    public static String getFilePathByTemplateVirtualPath(String gStorageId, String virtual_path) {
//		int length=WEB_ROOT.length()+uploadBashFilePath.length()+3;//3个斜杠
        String outPath = getSystemUploadPath(gStorageId) + virtual_path;
        if (!new File(outPath).isDirectory()) {
            new File(outPath).mkdirs();
        }
        return outPath;
    }

    /**
     * debug日志路径
     *
     * @return
     */
    public static String getPhysicalLogsPath(String id) {
        String path = LOGS_PHYSICAL_FILE_FOLDER(id);
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        return path;
    }


    /**
     * error日志路径
     *
     * @return
     */
    public static String getPhysicalS_LogsPath(String id ) {
        String path = S_LOGS_PHYSICAL_FILE_FOLDER(id);
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        return path;
    }

    //数据库备份日志
    public static String getPhysicalSqlLogPath(String id ) {
        return getPhysicalS_LogsPath(id) + DB_LOG;
    }

    //根据用户id获取用户文件物理存放路径
    public static String getFolderUserTimePath(String gStorageId, String userId, String timePath) {
        return  getSystemUploadPath(gStorageId)+  VIRTUAL_USER_FOLDER + userId + "/" + timePath + "/";
    }


    //获取日期图片物理存放路径
    public static String getPhysicalPicDirPath(String gStorageId,String userId) {
        String path = getSystemUploadPath(gStorageId)+  VIRTUAL_USER_FOLDER  + userId + "/";
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        return path;
    }
    //获取绝对导出路径
    	public static String getAbsoluDownLoadToExcelPath(String gStorageId,String verPath) {
		return  getSystemUploadPath(gStorageId)+ uploadBashFilePath + "/" + temp + "/" + verPath;
	}

    //二维码文件存放物理位置
    public static String getPhysicalQRCodeFolder(String gStorageId) {
        if (!new File(getSystemUploadPath(gStorageId)+VIRTUAL_QRCODE_FOLDER).exists()) {
            new File(getSystemUploadPath(gStorageId)+VIRTUAL_QRCODE_FOLDER).mkdirs();
        }
        return getSystemUploadPath(gStorageId)+VIRTUAL_QRCODE_FOLDER;
    }

    //各个公司的sql文件存放目录
	public static String getPhysicalSqlFolder(String gStorageId) {
		String path = getSystemUploadPath(gStorageId)+VIRTUAL_SQL_FOLDER;
		if (!new File(path).exists()) {
			new File(path).mkdirs();
		}
		return path;
	}

    //获取审批章的路径(cma专用)
    public static String getExaminePicPath(String vierPath) {
        return  uploadBashFilePath + "/" + images + "/" + vierPath;
    }

    /**
     * 根据相对路径取得文件的绝对路径
     *
     * @param virtual_path
     * @return
     */
    public static String getFilePathByVirtualPath(String gStorageId ,String virtual_path) {
//		int length=WEB_ROOT.length()+uploadBashFilePath.length()+3;//3个斜杠
        String outPath = getSystemUploadPath(gStorageId) + virtual_path;
        if (!new File(outPath).isDirectory()) {
            new File(outPath).mkdirs();
        }
        return outPath;
    }
}
